package com.util;

import cn.hutool.core.util.IdUtil;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5Util {

    public static void main(String[] args) {
		//String pwd = (MD5Util.getMD5("123456"));
//		MD5Util.hexStringToBytes("460C16990E1818E276559F49836A267D");
        //String s1 = MD5Util.md5sum("460C16990E1818E276559F49836A267D");
//		String s = MD5Util.base64Decode("460C16990E1818E276559F49836A267D");
        //String jiemi = MD5Util.jiemi("460C16990E1818E276559F49836A267D", s1);
        String pwd = MD5Util.getMD5(MD5Util.getMD5("123456"));

        System.out.println(IdUtil.fastSimpleUUID());

        //String md5 = MD5Util.md5sum("d:/a.png");
        //System.out.println(md5);
    }

    private static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};


    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_DIGITS[(b[i] & 240) >>> 4]);
            sb.append(HEX_DIGITS[b[i] & 15]);
        }
        return sb.toString();
    }

    private static String byteHEX(byte ib) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
                'B', 'C', 'D', 'E', 'F'};
        char[] ob = new char[2];
        ob[0] = Digit[(ib >>> 4) & 0X0F];
        ob[1] = Digit[ib & 0X0F];
        String s = new String(ob);
        return s;
    }

    public static String base64Decode(String src) {


        byte[] b = null;
        try {
            b = java.util.Base64.getDecoder().decode(src); //base64Decoder.decodeBuffer(src);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String result = new String(b);
        return result;
    }




    // 字符串加密
    public static String getMD5(String source) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        messageDigest.update(source.getBytes());
        byte[] b = messageDigest.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            sb.append(byteHEX(b[i]));
        }

        // sb.setCharAt(sb.length()-1, (char)(sb.charAt(sb.length()-1)+1));
        return sb.toString();
    }

    public static byte[] hexStringToBytes(String hexString) {

        if (hexString == null || hexString.equals("")) {

            return null;

        }

        hexString = hexString.toUpperCase();

        int length = hexString.length() / 2;

        char[] hexChars = hexString.toCharArray();

        byte[] d = new byte[length];

        for (int i = 0; i < length; i++) {

            int pos = i * 2;

            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

        }

        return d;
    }

    private static byte charToByte(char c) {

        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static String md5sum(String filename) {
        byte[] buffer = new byte[1024];
        try {
            InputStream fis = new FileInputStream(filename);
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            while (true) {
                int numRead = fis.read(buffer);
                if (numRead > 0) {
                    md5.update(buffer, 0, numRead);
                } else {
                    fis.close();
                    return MD5Util.toHexString(md5.digest());
                }
            }
        } catch (Exception e) {
            return null;
        }
    }

}
